package com.hz.ys_production.dao;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.supermanage.bean.Hzsyspersonnel;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.ys_stock.dao.PurchaseDao.java
 *     Description(说明)	:  供应商管理
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class TaskStartDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 查询库存 信息
	 */
	public String SelTaskStart(HttpServletRequest request ,String operate ){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append("select o.name,t.*, " );
		sbSQL.append("m.status taskstatus,m.createtime as starttime,m.orders,m.orderuser,m.createuser, ");
		sbSQL.append("p.machineid,p.machinename,p.operater,p.status as machstatus ");
		sbSQL.append(" from taskInfo t ");
		sbSQL.append(" left join orderinfo o on t.orderid = o.orderid ");
		String where = "";
//		已开工
		if("start".equals(operate)){
			//Hzsyspersonnel user=(Hzsyspersonnel)request.getSession().getAttribute("admin_user");
			sbSQL.append(" left join machinetask m on t.taskid= m.taskid and m.status ='已开工'");
			where = " where  t.status = '已开工' order by  CONVERT(m.orders,SIGNED),orderid,taskname,createtime"; //and m.createuser = '"+user.getPersonname()+"' 
		}
//		未开工
		if("notstart".equals(operate)){
			sbSQL.append(" left join machinetask m on t.taskid= m.taskid and m.status ='未开工'");
			where = " where  t.status != '已完工' and t.status != '已开工' order by IFNULL(CONVERT(m.orders,SIGNED),999),orderid,taskname,createtime";
		}
		sbSQL.append(" left join pres_machineinfo p on p.machineid =  m.machineid");
		sbSQL.append(where);
		
		System.out.println("++1++");
		return sbSQL.toString();
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param request
	 * @return 
	 * 方法功能:
	 */
	public boolean doStartTask(HttpServletRequest request){
		String taskid = CommonDao.isnull(request.getParameter("taskid"));
		String machineid = CommonDao.isnull(request.getParameter("machineid"));
		String createuser = CommonDao.isnull(request.getParameter("createuser"));
		String[] sql = {"update machinetask set status= '已开工', createtime ='"+DateUtil.getDateTime()+"',createuser='"+createuser+"' where taskid ='"+taskid+"'",
				"update taskinfo set status= '已开工' where taskid ='"+taskid+"'",
				"update pres_machineinfo set status= '繁忙' where machineid ='"+machineid+"'"};
		CommonDBDao dbo= new CommonDBDao();
		return dbo.insorupdArray(sql);
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 机台生产单信息
	 */
	public List doTaskPrint(HttpServletRequest request){
//		String[] taskids = request.getParameterValues("taskid");
//		String taskid ="";
//		if(taskids!=null){
//			for(int i=0;i<taskids.length;i++){
//				taskid+="'"+taskids[i]+"',";
//			}
//		}
		String machineid = CommonDao.isnull(request.getParameter("machineid"));
		String sql = "select o.orderid,t.taskid,t.taskname,o.ordertime,o.finishtime,g.goodsname,t.printsize,t.finishsize,t.amount,t.addamount," 
				+" t.ctp,t.singledouble,t.turnover,t.cut,o.finishrequest,t.description,o.creator,m.machineid,p.machinename from machinetask m "
				+" left join taskinfo t on t.taskid = m.taskid "
				+" left join orderinfo o on t.orderid = o.orderid "
				+" left join pres_makings g on g.goodsid = t.goodsid " 
				+" left join pres_machineinfo p on p.machineid = m.machineid"
				+" where m.machineid = '"+machineid+"' and checkflag='y'";
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonList(sql);
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 机台排产单
	 */
	public void doTaskListPrint(HttpServletRequest request){
		
		String machineid = CommonDao.isnull(request.getParameter("machineid"));
		String sql = "select m.*,o.orderid,o.name,o.ordertime,t.taskName,t.printSize,t.amount,t.addAmount,t.subPaper,t.goodsid,g.goodsname,g.standard from machinetask m " 
			+" left join taskinfo t on t.taskid = m.taskid "
			+" left join orderinfo o on t.orderid = o.orderid "
			+" left join pres_makings g on t.goodsid = g.goodsid "
			+" where m.machineid = '"+machineid+"' and checkflag='y' order by IFNULL(m.orders,999),t.orderid,t.taskname,t.createtime";
		
//		System.out.println(sql);
		CommonDBDao dbo= new CommonDBDao();
		request.setAttribute("listinfo", dbo.DBjsonList(sql));
		
		request.setAttribute("machineinfo", dbo.queryObjectByPS("select * from pres_machineinfo where machineid = ?", new SqlParameter(machineid),null));
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param request
	 * @return 
	 * 方法功能:
	 */
	public List doFinishOrderPrint(HttpServletRequest request){
		String[] supps = request.getParameterValues("supp");
		String orderid = "";
		if(supps!=null){
			for(int i=0;i<supps.length;i++){
				orderid+="'"+supps[i]+"',";
			}
		}
		orderid = orderid.equals("") ? "" : orderid.substring(0,orderid.lastIndexOf(","));
		String sql = "select c.oct,o.orderid,o.name as ordername,o.goodstype,o.otherClient,o.otherTel,o.otherAddress,o.deliverflag,o.delivertime,o.deliveruser,o.debtcash,o.isCollection," 
			+"t.taskid,t.taskname,t.finishSize,t.amount,c.clientName,c.phone,c.unitAddress  from orderinfo o "
			+" left join taskinfo t on t.orderid = o.orderid "
			+" left join clientinfo c on c.clientid = o.clientid " 
			+" left join (select count(orderid) as oct,orderid from taskinfo group by orderid ) as c on c.orderid = o.orderid"
			+" where o.orderid in ("+orderid+") order by o.orderid ";
//		System.out.println(sql);
		CommonDBDao dbo= new CommonDBDao();
		List listinfo = null;
		if(!"".equals(orderid)){
			listinfo = dbo.DBjsonList(sql);
			String username = ((Hzsyspersonnel) request.getSession().getAttribute("admin_user")).getPersonname();
			dbo.insorupd("update orderinfo set deliverflag='load',delivertime='"+DateUtil.getDateTime()+"',deliveruser='"+username+"' where orderid in ("+orderid+")");
		}
		return listinfo;
	}
}
